﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Configuration;
using System.Text;
using KPIS.GERP.SERVICE.DAL;
using KPIS.GERP.SERVICE.Model;
using KPIS.DBM;
using System.Data;

namespace KPIS.GERP.SERVICE.DAL
{
    public class AbsenceTypes
    {
        Common common = new Common();
        DBManager dbManager = new DBManager(DataProvider.MySQL, ConfigurationSettings.AppSettings["ConnectionString"]);
       
        public List<hrm_ps_absence_types> GetAllAbsenceTypes()
        {
            List<hrm_ps_absence_types> result = new List<hrm_ps_absence_types>();
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(1);
                dbManager.AddParameters(0, "@delete_status", Common.DeletedStatus);
                const string strSQL = "select * from hrm_ps_absence_types where record_status <> @delete_status";
                // + (!string.IsNullOrWhiteSpace(start_period) ? " and non_working_date = @start_period" : "");

                DataTable dtb = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0];
                foreach (DataRow dr in dtb.Rows)
                {
                    result.Add(new hrm_ps_absence_types()
                    {
                        absence_type_seq = Convert.ToInt32(dr["absence_type_seq"].ToString()),
                        absence_type_name = dr["absence_type_name"].ToString(),
                        absence_type_group_seq = common.ConvertToNullableInt(dr["absence_type_group_seq"].ToString()),
                        day_count_advance = common.ConvertToNullableInt(dr["day_count_advance"].ToString()),
                        //รวมวันหยุด 1 = รวมวันหยุด, 0 = ไม่รวมวันหยุด
                        include_nwd = dr["include_nwd"].ToString(),
                        remarks = dr["remarks"].ToString(),
                        record_status = dr["record_status"].ToString(),
                        created_by = common.ConvertToNullableInt(dr["created_by"].ToString()),
                        created_when = common.ConvertToNullableDateTime(dr["created_when"].ToString()),
                        updated_by = common.ConvertToNullableInt(dr["updated_by"].ToString()),
                        updated_when = common.ConvertToNullableDateTime(dr["updated_when"].ToString())
                    });
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (dbManager.Connection != null) { dbManager.Connection.Close(); dbManager.Dispose(); }
            }
            return result;
        }
    }
}
